﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        LoadProduct();
        if (Session["adminlogin"] == null)
        {
            Response.Redirect("adminlogin.aspx");
        }
    }
    protected void LoadProduct()
    {
        ProductBus proBus = new ProductBus();
        DataTable dt = new DataTable();
        dt = proBus.GetAllProduct();
        ClPageProduct.BindToControl = grvProduct;
        ClPageProduct.DataSource = dt.DefaultView;
        grvProduct.DataSource = ClPageProduct.DataSourcePaged;
        grvProduct.DataBind();
    }

    protected void grvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int pdID = Convert.ToInt32(grvProduct.DataKeys[e.RowIndex].Value);

        ProductBus proBus = new ProductBus();
        if (!proBus.DeleteProduct(pdID))
            Response.Write("<script>alert('This product contains references')</script>");
        else
            //xoa xong load len
            LoadProduct();
    }
    protected void LinkBEdit_Command(object sender, CommandEventArgs e)
    {
        int pdID = Convert.ToInt32(e.CommandArgument);
        Response.Redirect("EditProduct.aspx?ID="+pdID);
    }

    protected void ButtonProduct_Click(object sender, EventArgs e)
    {
        if (DropDownListSearchPr.SelectedIndex == 0)
        {
            ProductBus proBus = new ProductBus();
            DataTable dt = new DataTable();
            dt = proBus.SearchProduct(TextBoxSrch.Text);
            ClPageProduct.BindToControl = grvProduct;
            ClPageProduct.DataSource = dt.DefaultView;
            grvProduct.DataSource = ClPageProduct.DataSourcePaged;
            grvProduct.DataBind();
        }
        else if (DropDownListSearchPr.SelectedIndex == 1)
        {
            ProductBus proBus = new ProductBus();
            DataTable dt = new DataTable();
            dt = proBus.GetProductByID(Convert.ToInt32(TextBoxSrch.Text));
            ClPageProduct.BindToControl = grvProduct;
            ClPageProduct.DataSource = dt.DefaultView;
            grvProduct.DataSource = ClPageProduct.DataSourcePaged;
            grvProduct.DataBind();
        }
    }
}
